Page({
  data: {
    examList: [],
    page: 1,
    pageSize: 10,
    isLast: false
  },
  onLoad: function() {
    this.getExamList()
  },
  getExamList: function() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    wx.cloud.callFunction({
      name: 'getExamList',
      data: {
        page: this.data.page,
        pageSize: this.data.pageSize
      },
      success: function(res) {
        wx.hideLoading()
        that.setData({
          examList: that.data.examList.concat(res.result.data)
        })
        if (res.result.data.length < that.data.pageSize) {
          that.setData({
            isLast: true
          })
        }
      },
      fail: function(res) {
        console.log(res)
      }
    })
  },
  addExam: function() {
    wx.redirectTo({
      url: '../addExam/addExam',
    })
  },
  onReachBottom: function () {
    if (this.data.isLast === false) {
      this.setData({
        page: this.data.page + 1
      })
      this.getExamList()
    } else {
      wx.showToast({
        title: '已经没有数据了...',
        icon: 'none'
      })
    }
  },
  remove: function(e) {
    var that = this
    wx.showModal({
      title: '警告',
      content: '是否确定删除该信息',
      success: function(res) {
        if(res.confirm === true) {
          wx.showLoading({
            title: '请稍后',
          })
          wx.cloud.callFunction({
            name: 'removeExam',
            data: {
              _id: e.currentTarget.dataset._id
            },
            success: function(res) {
              wx.hideLoading()
              that.data.examList.splice(e.currentTarget.dataset.index, 1)
              that.setData({
                examList: that.data.examList
              })
              wx.showToast({
                title: '删除成功',
              })
            },
            fail: function(res) {
              console.log(res)
            }
          })
        }
      }
    })
  },
  edit: function(e) {
    wx.redirectTo({
      url: '../editExam/editExam?_id=' + e.currentTarget.dataset._id,
    })
  }
})